import { reactive } from 'vue'
import { format } from 'date-fns'
import { parseTime } from '@/common/common'
import { Project } from '@/service/modules/project/types'
import {
  findPublicDatabasesReq,
  syncDbMetaReq
} from '@/service/modules/data-manage'
import { DatabaseInfo } from '@/service/modules/data-manage/types'
import { useRouter } from 'vue-router'

export function useTable() {
  const router = useRouter()
  const state = reactive({
    page: 1,
    limit: 10,
    count: 0,
    searchParam: {
      datasourceType: null,
      datasourceId: null,
      searchKey: null
    },
    list: [],
    loading: false,
    currentRecord: {} as DatabaseInfo,
    modDbRemarkModalShow: false,
    modDbOwnerModalShow: false,
    dropDbModalShow: false
  })

  const getList = async () => {
    if (state.loading) return
    state.loading = true

    const { data, count } = await findPublicDatabasesReq({
      page: state.page,
      limit: state.limit,
      ...state.searchParam
    })
    state.loading = false
    if (!data) throw Error()
    state.list = data.map((record: Project) => {
      record.createTime = record.createTime
        ? format(parseTime(record.createTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      record.updateTime = record.updateTime
        ? format(parseTime(record.updateTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      return record
    })
    state.count = count
  }

  const updateList = () => {
    if (state.list.length === 1 && state.page > 1) {
      --state.page
    }
    getList()
  }

  const openTableListTab = (
    dsType: string,
    datasourceId: number,
    dsName: string,
    databaseId: number,
    dbName: string
  ) => {
    router.push({
      path: '/dataManage/dataCatalog/tableList',
      query: {
        tab:
          window.$t('dataManage.dbName') +
          `:${dbName} ` +
          window.$t('dataManage.tableList'),
        tabName: 'TableListTab',
        dsType: dsType,
        datasourceId: datasourceId,
        dsName: dsName,
        dbName: dbName,
        databaseId: databaseId
      }
    })
  }

  const syncDbMeta = async (databaseId: number) => {
    if (state.loading) return
    await syncDbMetaReq({ databaseId: databaseId })
    state.loading = false
  }

  const onOperationClick = (
    data: { rowData: DatabaseInfo },
    type:
      | 'modDbRemark'
      | 'modDbOwner'
      | 'openTableListTab'
      | 'syncDbMeta'
      | 'dropDb'
  ) => {
    state.currentRecord = data.rowData
    switch (type) {
      case 'modDbRemark':
        state.modDbRemarkModalShow = true
        break
      case 'modDbOwner':
        state.modDbOwnerModalShow = true
        break
      case 'openTableListTab':
        openTableListTab(
          data.rowData.datasourceType,
          data.rowData.datasourceId,
          data.rowData.datasourceName,
          data.rowData.id,
          data.rowData.dbName
        )
        break
      case 'syncDbMeta':
        syncDbMeta(data.rowData.id)
        break
      case 'dropDb':
        state.dropDbModalShow = true
        break
    }
  }

  const changePage = (page: number) => {
    state.page = page
    getList()
  }

  const changePageSize = (limit: number) => {
    state.page = 1
    state.limit = limit
    getList()
  }

  return {
    state,
    changePage,
    changePageSize,
    updateList,
    onOperationClick
  }
}
